package com.jyxd.web.task;

import com.jyxd.web.data.basic.QuartzTask;
import com.jyxd.web.data.basic.QuartzTime;
import com.jyxd.web.service.assessment.basic.QuartzTaskService;
import com.jyxd.web.service.assessment.basic.QuartzTimeService;
import com.jyxd.web.service.czform.CRRTNurseService;
import com.jyxd.web.util.CompareTimeUtil;
import com.jyxd.web.util.SpringUtil;
import com.jyxd.web.util.UUIDUtil;
import org.quartz.CronExpression;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * crrtnurse自动生成job
 */
public class CrrtNurseJob implements Job {

    private static Logger logger = LoggerFactory.getLogger(CrrtNurseJob.class);

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("====================开始同步crrt护理单数据");
        Date date = new Date();
        QuartzTime quartzTime = new QuartzTime();
        quartzTime.setStartTime(date);
        ServletContext context = null;
        try {
            context = (ServletContext) jobExecutionContext.getScheduler().getContext().get(QuartzServletContextListener.MY_CONTEXT_NAME);
            WebApplicationContext cxt = (WebApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
            QuartzTimeService quartzTimeService = (QuartzTimeService) cxt.getBean("quartzTimeService");
            QuartzTaskService quartzTaskService = (QuartzTaskService) cxt.getBean("quartzTaskService");
            CRRTNurseService crrtNurseService = SpringUtil.getBean(CRRTNurseService.class);
            crrtNurseService.batchSync(date);
            logger.info("====================同步crrt护理单数据结束");

            //操作定时任务执行记录表
            Map<String, Object> map = new HashMap<>();
            map.put("jobName", "CrrtNurseJob");
            map.put("jobGroup", "CrrtNurseJob");
            QuartzTask quartzTask = quartzTaskService.queryDataByNameAndGroup(map);
            if (quartzTask != null) {
                CronExpression expression = new CronExpression(quartzTask.getCron());
                quartzTime.setNextTime(expression.getNextValidTimeAfter(quartzTime.getStartTime()));
                quartzTime.setQuartzTaskId(quartzTask.getId());
                quartzTime.setId(UUIDUtil.getUUID());
                quartzTime.setEndTime(new Date());
                quartzTime.setTimeLength(String.valueOf(CompareTimeUtil.calculatetimeGapSecond(quartzTime.getStartTime(), quartzTime.getEndTime())));
                quartzTime.setStatus(1);
                quartzTimeService.insert(quartzTime);
            }
        } catch (Exception e) {
            logger.error("====================同步crrt护理单数据异常");
            e.printStackTrace();
        }
    }
}
